/* $Id: listzone.js 2383 2006-11-05 10:43:02Z scottye $ */

var listZone = new Object();

listZone.filter         = new Object();
listZone.orgObject      = null;
listZone.orgContent     = null;
listZone.loadFunc       = null;
listZone.pageCount      = 0;
listZone.pageCurrent    = 0;

listZone.filename = location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("?")) + "?act=ajax";

/**
 * 显示正在载入
 */
listZone.showLoader = function()
{
    listZone.toggleLoader(true);
}

listZone.hideLoader = function()
{
    listZone.toggleLoader(false);
}

listZone.toggleLoader = function(disp)
{
    document.getElementsByTagName('body').item(0).style.cursor = (disp) ? "wait" : 'auto';
    
    var doc = top.frames['header-frame'].document;
    var loader = doc.getElementById("load-div");

    loader.style.display = disp ? "block" : "none";
}

listZone.loadList = function(func)
{
    listZone.showLoader();
    listZone.loadFunc = func;

    cp.set_response_type("object");
    cp.call(listZone.filename, func, listZone.response, listZone.filter);
}

/**
 * 将返回的数据解析为表格的形式
 * 
 * @param: result   返回的数据
 *
 * @return null
 */
listZone.response = function(result, text_result)
{
    listZone.hideLoader();
    layer = document.getElementById("listDiv");
    layer.innerHTML = "";

    tbl = document.createElement("TABLE");
    tbl.cellSpacing = 1;
    tbl.cellPadding = 3;
    row = tbl.insertRow(-1);

    /* 创建表头 */
    thead = result.ajaxResponse[0].list[0].thead[0];
    for(i = 0; i < thead.item.length; i++)
    {
        cell = document.createElement("TH");
        
        txt = "";

        // 是否在前面加上checkbox
        if (thead.item[i].get_attribute("select_all"))
        {
            txt += "<input type='checkbox' onclick='listZone.selectAll(this, \"checkboxes\")' />";
        }

        // 是否可以排序
        if (thead.item[i].get_attribute("sort_by") != null)
        {
            txt += "<a href=\"javascript:listZone.sort('" +thead.item[i].get_attribute("sort_by")+ "', '" +thead.item[i].get_attribute("sort_order")+ "');\">" + thead.item[i].data + "</a>";
        }
        else
        {
            txt += thead.item[i].data;
        }

        if (thead.item[i].get_attribute("sort_this"))
        {
            if (thead.item[i].get_attribute("sort_order") == "DESC")
            {
                txt += "<img src='images/sort_desc.gif' width='9' height='9' alt='DESC' />";
            }
            else
            {
                txt += "<img src='images/sort_asc.gif' width='9' height='9' alt='ASC' />";
            }
        }

        cell.innerHTML = txt;
        row.appendChild(cell);
    }

    /* 创建数据列表 */
    items = result.ajaxResponse[0].list[0].items[0];
    if (items.row)
    {
        for (i = 0; i < items.row.length; i++)
        {
            row = tbl.insertRow(-1);
            idx = items.row[i].get_attribute("id");

            for (j = 0; j < items.row[i].item.length; j++)
            {
                cell = row.insertCell(-1);
                
                // 对齐方式
                if (items.row[i].item[j].get_attribute("align") != null)
                {
                    cell.align = items.row[i].item[j].get_attribute("align");
                }

                txt = '<span';
                // 是否可以编辑
                type = items.row[i].item[j].get_attribute("type");
                handler = items.row[i].item[j].get_attribute("handler");
                if (handler != null && type > 0)
                {
                    if (type == 1)
                    {
                        txt += ' onclick="editZone.edit(this, \'' + handler + '\', ' + idx + ')"';
                    }
                    else
                    {
                        txt += '><a href="javascript:;" onclick="listZone.toggle(this, \'' + handler + '\', ' + idx + ')"';
                    }
                }
                txt += '>';

                // 是否带有checkbox
                if (items.row[i].item[j].get_attribute("checkbox") == 1)
                {
                    txt += "<input type='checkbox' value='" + items.row[i].item[j].data + "' name='checkboxes[]' />";
                }

                txt += items.row[i].item[j].data;

                if (handler != null && type > 0)
                {
                    txt += '</a>';
                }

                txt += '</span>';

                cell.innerHTML = txt;
            }
        }
    }
    
    /* 翻页部分 */
    page_turner = result.ajaxResponse[0].list[0].pageTurner[0];
    
    listZone.pageCount      = parseInt(page_turner.pageCount[0].data);
    listZone.pageCurrent    = parseInt(page_turner.pageCurrent[0].data);

    if (listZone.pageCount == 0) listZone.pageCount = 1;

    document.getElementById("totalRecords").innerHTML   = page_turner.recordCount[0].data;
    document.getElementById("totalPages").innerHTML     = page_turner.pageCount[0].data;
    document.getElementById("pageCurrent").innerHTML    = page_turner.pageCurrent[0].data;
    document.getElementById("pageSize").value           = page_turner.pageSize[0].data;

    /* 页面跳转下拉框 */
    try
    {
    	var selPage = document.getElementById("gotoPage");
      selPage.length = 0;

      for (i = 1; i <= listZone.pageCount; i++)
      {
        var opt = document.createElement("OPTION");
        opt.value = i;
        opt.text = i;

        if (i == listZone.pageCurrent)
        {
          opt.selected = true;
        }
        Browser.isIE ? selPage.add(opt) : selPage.appendChild(opt);
      }
    }
    catch (e)
    {
      alert(e);
    }

    /* 记录过滤条件 */

    filter = result.ajaxResponse[0].list[0].filter[0];
    listZone.filter = new Object();
    
    listZone.filter['page']         = page_turner.pageCurrent[0].data;
    listZone.filter['page_size']    = page_turner.pageSize[0].data;

    if (filter.item)
    {
        for (i = 0; i < filter.item.length; i++)
        {
            listZone.filter[filter.item[i].get_attribute("name")] = filter.item[i].data;
        }
        
    }
    else
    {
        listZone.filter.length = 0;
    }
    //alert(text_result);
    //alert(tbl.innerHTML);
    layer.appendChild(tbl);
}

/** 
 * 排序字段的函数
 * 
 * @param: sortBy[string]       排序的字段
 * @param: sortOrder[string]    排列方式 DESC or ASC
 */
listZone.sort = function(sortBy, sortOrder)
{
    listZone.filter['sort_by']      = sortBy;
    listZone.filter['sort_order']   = (sortOrder != "DESC") ? "DESC" : "ASC";

    listZone.pageSize();
    listZone.loadList(listZone.loadFunc);
}

/** 
 * 翻页函数
 * 
 * @param: page[int]    页数
 */
listZone.gotoPage = function(page)
{
    if (page!=null) listZone.filter['page'] = page;

    listZone.pageSize();
    listZone.loadList(listZone.loadFunc);
}

listZone.gotoPageFirst = function()
{
    if (listZone.pageCurrent > 1)
    {
        listZone.gotoPage(1);
    }
}

listZone.gotoPagePrev = function()
{
    if (listZone.pageCurrent > 1)
    {
        listZone.gotoPage(listZone.pageCurrent - 1);
    }
}

listZone.gotoPageNext = function()
{
    if (listZone.pageCurrent < listZone.pageCount)
    {
        listZone.gotoPage(listZone.pageCurrent + 1);
    }
}

listZone.gotoPageLast = function()
{
    if (listZone.pageCurrent < listZone.pageCount)
    {
        listZone.gotoPage(listZone.pageCount);
    }
}

listZone.pageSize = function()
{
    pageSize = document.getElementById("pageSize");

    if (pageSize)
    {
        listZone.filter['page_size'] = pageSize.value;
    }
}

/**
 * 切换是否
 */
listZone.toggle = function(obj, handler, id) 
{
    listZone.orgObject = obj;
    listZone.orgContent = obj.innerHTML;
    listZone.showLoader();

    cp.set_response_type('object');
    cp.call(listZone.filename, handler, listZone.responseToggle, id, listZone.orgContent);
}

/**
 * 接收切换是否函数的回馈信息
 */
listZone.responseToggle = function(result)
{
    listZone.hideLoader();

    if (listZone.orgObject == null) return;

    var reval = result.ajaxResponse[0].newvalue[0].data;

    /* 解析返回的数据 */
    if (result.ajaxResponse[0].success[0].data == "TRUE") 
    {
        // 操作成功了
        listZone.orgObject.innerHTML = reval;
    }
    else
    {
        // 操作失败了
        listZone.orgObject.innerHTML = listZone.orgContent;
    }

    /* 如果返回了消息 */
    if (result.ajaxResponse[0].message[0].data.length > 0) 
    {
        alert(result.ajaxResponse[0].message[0].data);
    }
    
    listZone.orgObject = null;
    listZone.orgContent = null;
}

/**
 * 解析反馈信息
 *
 * @param: result       反馈的结果
 * @param: success_func 如果反馈的结果是成功，则调用这个函数
 * @param: failed_func  如果反馈的结果是失败，则调用这个函数
 */
listZone.parseResult = function(result, success_func, failed_func, need_confirm)
{
    listZone.hideLoader();

    success     = eval(result.ajaxResponse[0].success[0].data.toLowerCase());
    message     = result.ajaxResponse[0].message[0].data;
    newvalue    = result.ajaxResponse[0].newvalue[0];

    if (success)
    {

        if (message.length > 0)
        {
            if (need_confirm)
            {
                if (success_func && confirm(message)) success_func(newvalue);
            }
            else
            {
                alert(message);
                if (success_func) success_func(newvalue);
            }
        }
        else
        {
            if (success_func) success_func(newvalue);
        }
    }
    else
    {
        if (failed_func) failed_func();

        if (message.length > 0)
        {
            alert(message);
        }
    }
}

listZone.selectAll = function(obj, objName)
{
    var checkboxes = document.getElementsByTagName("input");

    for (i = 0; i < checkboxes.length; i++)
    {
        if (checkboxes[i].name == objName + "[]")
        {
            checkboxes[i].checked = obj.checked;
        }
    }
}

/**
 * 移除列表中的一行
 * 
 * @param: anchor           点击的链接对象
 * @param: handler          处理删除事件的PHP函数
 * @param: recordId         要删除的记录的 id
 * @param: removeConfirm    请求用户确认的消息
 *
 * @return 
 */
listZone.removeRow = function(anchor, handler, recordId, removeConfirm)
{
    // 请求用户确认
    if (!confirm(removeConfirm))
    {
        return;
    }

    listZone.orgObject = anchor.parentNode.parentNode;
    
    if (listZone.orgObject.tagName == "TR") 
    {
        cp.call(listZone.filename, handler, listZone.removeRowResponse, recordId);
		return;
    }
	
	if (listZone.orgObject.parentNode.tagName == "TR")
	{
		listZone.orgObject = listZone.orgObject.parentNode;
        cp.call(listZone.filename, handler, listZone.removeRowResponse, recordId);
	}
    else
    {
        listZone.orgObject = null;
    }
}

listZone.removeRowResponse = function(result)
{
    success     = eval(result.ajaxResponse[0].success[0].data.toLowerCase());
    message     = result.ajaxResponse[0].message[0].data;
    newvalue    = result.ajaxResponse[0].newvalue[0].data;

    if (success)
    {
        // 删除行
        var table = listZone.orgObject.parentNode;
        for (i = 0; i < table.childNodes.length; i++)
        {
            if (table.childNodes[i] == listZone.orgObject)
            {
                table.removeChild(table.childNodes[i]);
            }
        }
    }

    if (message.length > 0)
    {
        alert(message);
    }
}